using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Windows.Forms;

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using System.Threading;
using System.ComponentModel;
using EntLibContrib.PolicyInjection.CallHandlers;
using System.Transactions;

namespace EntLibContrib.PolicyInjection.Tests
{
    [TestClass]
    public class TransactionScopeCallHandlerFixture
    {
        [TestMethod, DeploymentItem("TransactionScopeCallHandler.config")]
        public void TransactionScope_Custom_With_Config()
        {
            IConfigurationSource configurationSource = new FileConfigurationSource("TransactionScopeCallHandler.config");

            TransactionScopeFoo foo = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Create<TransactionScopeFoo>(configurationSource);

            foo.Do();
        }

        [TestMethod]
        public void TransactionScope_Default_With_Attributes()
        {
            TransactionScopeFooWithAttributes foo = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Create<TransactionScopeFooWithAttributes>();

            foo.DoDefault();
        }

        [TestMethod]
        public void TransactionScope_Custom_With_Attributes()
        {
            TransactionScopeFooWithAttributes foo = Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection.Create<TransactionScopeFooWithAttributes>();

            foo.DoCustom();
        }
    }

    public class TransactionScopeFoo : MarshalByRefObject
    {
        public void Do()
        {
            Assert.IsNotNull(Transaction.Current, "No transaction scope created");
            Assert.AreEqual(IsolationLevel.Snapshot, Transaction.Current.IsolationLevel);
        }
    }

    public class TransactionScopeFooWithAttributes : MarshalByRefObject
    {
        [TransactionScopeCallHandler(
            ScopeOption = TransactionScopeOption.Required,
            IsolationLevel = IsolationLevel.Snapshot,
            Timeout="00:00:30",
            Complete = false,
            InteropOption = EnterpriseServicesInteropOption.None)]
        public void DoCustom()
        {
            Assert.IsNotNull(Transaction.Current, "No transaction scope created");
            Assert.AreEqual(IsolationLevel.Snapshot, Transaction.Current.IsolationLevel);
        }

        [TransactionScopeCallHandler]
        public void DoDefault()
        {
            Assert.IsNotNull(Transaction.Current, "No transaction scope created");
            Assert.AreEqual(IsolationLevel.Serializable, Transaction.Current.IsolationLevel);
        }
    }
}

